package org.mytools.dao.gener.meta;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MYSQLMetaData extends AbstarctMetaData {

	public MYSQLMetaData(Connection connection) {
		super(connection);
	}

	@Override
	public String getDBName() {
		return "mysql";
	}

	@Override
	public List<String> getPKs(String tableName) throws SQLException {
		List<String> pksList = new ArrayList<String>();

		ResultSet restPK = this.databaseMetaData.getPrimaryKeys(null, null,
				tableName);
		while (restPK.next()) {
			pksList.add(restPK.getString(4));
		}
		close(restPK);

		return pksList;
	}

	@Override
	public List<ForeginKeys> getForeginKeys(String tableName)
			throws SQLException {
		List<ForeginKeys> pksList = new ArrayList<ForeginKeys>();

		ResultSet restFK = this.databaseMetaData.getImportedKeys(null, null,
				tableName);
		while (restFK.next()) {
			ForeginKeys fks = new ForeginKeys();
			fks.setFkTableName(restFK.getString(3));
			fks.setPkColumnName(restFK.getString(4));
			fks.setPkTableName(restFK.getString(7));
			fks.setFkColumnName(restFK.getString(8));

			pksList.add(fks);
		}

		return pksList;
	}

}
